package com.yanqu.road.server.logic.activity.condition.rank;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.passroad.PassRoadPointInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserPassRoad;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.passRoad.PassRoadModule;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseRankActivityCondition;
import com.yanqu.road.server.manger.config.PassRoadMgr;

import java.math.BigInteger;

/**
 * 关卡冲榜 冲榜活动
 */
public class PassThroughRankCondition extends BaseRankActivityCondition {

	public PassThroughRankCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
		super(activityInfo, activityConditionInfo, baseActivityData);
	}

	@Override
	public void addTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.PassThroughRank.getValue(), handle);
	}

	@Override
	public void removeTrigger(GamePlayer player) {
		player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.PassThroughRank.getValue(), handle);
	}

	@Override
	public void initDefaultValue() {
		UserPassRoad userPass = player.getModule(PassRoadModule.class).getUserPassRoad();
		if(null != userPass){
			setParam(userPass.getPassId() + "_" + userPass.getPassProgress());
			setValue(BigInteger.valueOf(PassRoadMgr.parseRoadProgressId(userPass.getPassId(), userPass.getPassProgress())));
			//重新生成排行榜
			rankChange();
		}
	}

	@Override
	protected void player_BoardLevelHandle(Object object) {
		UserPassRoad userPass = (UserPassRoad)object;
		if(null != userPass){
			PassRoadPointInfo info = PassRoadMgr.getPassRoadPointInfo(userPass.getPassId(),userPass.getPassProgress());
			if(info == null){
				info = PassRoadMgr.getLastPassRoadPointInfo();
			}
			setParam(info.getId() + "_" + info.getSessionId() + "_" + info.getSessionSectionId());
			setValue(BigInteger.valueOf(PassRoadMgr.parseRoadProgressId(userPass.getPassId(), userPass.getPassProgress())));
			//重新生成排行榜
			rankChange();
		}
	}
}
